/****************************************************************************************
*	Mouzetrap - client/server framework	
*
*	$HeadURL: http://mouzetrap.googlecode.com/svn/trunk/Mouzetrap/FixedObjectPool.cs $
*	$Id: FixedObjectPool.cs 12 2009-06-12 10:49:38Z tobz@fallenrealms.net $
* 
*	Copyright (c) 2008 Toby Lawrence
* 
*	Permission is hereby granted, free of charge, to any person obtaining a copy
*	of this software and associated documentation files (the "Software"), to deal
*	in the Software without restriction, including without limitation the rights
*	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*	copies of the Software, and to permit persons to whom the Software is
*	furnished to do so, subject to the following conditions:
* 
*	The above copyright notice and this permission notice shall be included in
*	all copies or substantial portions of the Software.
* 
*	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*	THE SOFTWARE.
* 
****************************************************************************************/

using System.Collections.Generic;
using System.Threading;
using Mouzetrap.Threading;

namespace Mouzetrap
{
	/// <summary>
	/// Provides a bounded object pool.  This pool is fixed in size.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <remarks>
	/// The pool will block on acquisitions when the pool is empty.
	/// </remarks>
	internal class FixedObjectPool<T> : IObjectPool
		where T : class
	{
		private readonly int _capacity;
		private readonly Stack<T> _poolObjects;
		private readonly Semaphore _poolSlots;
		private int _availableObjects;
		private SpinWaitLock _syncLock;

		internal FixedObjectPool(int capacity)
		{
			_syncLock = new SpinWaitLock();
			_poolObjects = new Stack<T>();
			_poolSlots = new Semaphore(0, capacity);
			_capacity = capacity;
		}

		#region IObjectPool Members

		/// <summary>
		/// The maximum number of objects this pool can hold.
		/// </summary>
		/// <remarks>This will either be a positive integer for fixed-size pools, or 
		/// -1 if the pool has no fixed size.</remarks>
		public int MaximumObjects
		{
			get { return _capacity; }
		}

		/// <summary>
		/// The number of available objects in the pool.
		/// </summary>
		public int AvailableObjects
		{
			get { return _availableObjects; }
		}

		/// <summary>
		/// The total number of objects known to exist by the pool.
		/// </summary>
		public int TotalObjects
		{
			get { return _capacity; }
		}

		/// <summary>
		/// Adds an object into the pool.
		/// </summary>
		/// <param name="poolObj">the object to add to the pool</param>
		public void Fill(object poolObj)
		{
			_syncLock.Enter();

			try
			{
				if(_availableObjects == _capacity)
					return;

				if(!(poolObj is T))
					return;

				_poolObjects.Push((T) poolObj);
				_availableObjects++;
				_poolSlots.Release();
			}
			finally
			{
				_syncLock.Exit();
			}
		}

		/// <summary>
		/// Retrieves the first available object from the pool.
		/// </summary>
		/// <remarks>This may or may not create a new object if none are available, 
		/// depending on implementation.</remarks>
		/// <returns>true if an object could be returned, false otherwise</returns>
		public object Drain()
		{
			_poolSlots.WaitOne();

			_syncLock.Enter();

			try
			{
				_availableObjects--;

				return _poolObjects.Pop();
			}
			finally
			{
				_syncLock.Exit();
			}
		}

		#endregion
	}
}